package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.itheima.Emtity.User;
import com.itheima.Utils.SMSUtils;
import com.itheima.Utils.ValidateCodeUtils;
import com.itheima.common.R;
import com.itheima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private HttpSession session;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 登录之后传送过来phone和code
     * 移动端的登录.
     *
     * @param user
     * @return
     */

    @PostMapping("login")
    public R<User> logind(@RequestBody Map map, HttpSession session, HttpServletRequest httpServletRequest) {
//        可以使用dto接收.也可以用Map
// map可以接受key:value的   接收json的存入.
        ValueOperations valueOperations = redisTemplate.opsForValue();

//        输入的手机和验证码
        String phone = (String) map.get("phone");
        String code = (String) map.get("code");

//        获取session的手机号码和验证码

//        String code_s = (String) session.getAttribute(phone);

//        根据手机号码,获得redis的验证码.
        Object code_s = valueOperations.get(phone);
        System.out.println(code_s);
//          验证码一样就登录成功.
        if (code_s != null && code_s.equals(code)) {
//            验证码一样.
            LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
            lambdaQueryWrapper.eq(User::getPhone, phone);
            User one = userService.getOne(lambdaQueryWrapper);

            User user = null;
            //        判断是新的用户吗?是的话保存数据.
            if (one == null) {
                user = new User();
                user.setPhone(String.valueOf(phone));
                user.setStatus(1);
                userService.save(user);
            }

//          新用户已经保存进去了.
            User one1 = userService.getOne(lambdaQueryWrapper);
//            获得保存进去的id.
//            设置登录状态,说明已经登录.
            httpServletRequest.getSession().setAttribute("user", one1.getId());
//            登录成功就删除验证码.
            redisTemplate.delete(phone);
            return R.success(user);
        }
        return R.error("登录失败");
//        进行对比
    }

    /**
     * 生成和发送验证码和存储验证码.
     *
     * @param user
     * @return
     */
    @PostMapping("sendMsg")
    public R<String> sendMes(@RequestBody User user) {
        ValueOperations valueOperations = redisTemplate.opsForValue();

//      获得手机号
        String phone = user.getPhone();
        if (StringUtils.isNotEmpty(phone)) {
//生成的验证码code
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info(code);
//            调用阿里云api
//            SMSUtils.sendMessage("ruiji", "", phone, code);
//          保存到session,  {1392449:2255}
//            session.setAttribute(phone, code);
            valueOperations.set(phone, code, 20, TimeUnit.SECONDS);

            return R.success("发送成功");
        }
        return R.error("发送失败");
    }
}
